package com.sunshine.lnuplus.utils

import android.content.Context
import android.graphics.Bitmap
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur


/**
 * Created by Anenn 简书
 * 高斯模糊类
 */
object BlurBuilder {
    fun blur(
        context: Context?,
        inputBitmap: Bitmap?,
        mohu: Float
    ): Bitmap {
        if(mohu <= 0){
            return inputBitmap!!
        }else{
            val outputBitmap = Bitmap.createBitmap(inputBitmap!!)
            val renderScript = RenderScript.create(context)
            val scriptIntrinsicBlur =
                ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript))
            val tmpIn = Allocation.createFromBitmap(renderScript, inputBitmap)
            val tmpOut = Allocation.createFromBitmap(renderScript, outputBitmap)
            scriptIntrinsicBlur.setRadius(mohu)
            scriptIntrinsicBlur.setInput(tmpIn)
            scriptIntrinsicBlur.forEach(tmpOut)
            tmpOut.copyTo(outputBitmap)
            return outputBitmap
        }
    }
}